/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package etf.pia.controlers;

import etf.pia.entity.Korisnik;
import etf.pia.entity.Predmet;
import etf.pia.util.Initializer;
import etf.pia.util.ServerUtil;
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author stefan
 */
@Getter
@Setter
@ManagedBean(name = "loginMediator")
@SessionScoped
public class UserBean implements Serializable {

    private String username;
    private String currUserLabel = "";
    private String password;
    private boolean profLogedIn = false;
    private boolean studentLogedIn = false;
    private boolean adminLogedIn = false;
    private Korisnik currUser;

    /**
     * Creates a new instance of LoginBean
     */
    public UserBean() {
        username = null;
        password = null;
        currUser = null;
    }

    public String attempLogin() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Criteria cr = ses.createCriteria(Korisnik.class);
            cr.add(Restrictions.eq("korisnickoIme", username));
            cr.add(Restrictions.eq("lozinka", password));

            currUser = (Korisnik) cr.uniqueResult();

            if (currUser == null) {
                t.commit();
                return "loginFailed?faces-redirect=true";
            } else {
                if (currUser.getRole().getDescription().equals("zaposleni")) {
                    Hibernate.initialize(currUser.getZaposleni());
                    Hibernate.initialize(currUser.getRole());
                    Initializer.initPredmets(currUser.getZaposleni());
                    for (Predmet p : Initializer.toList(currUser.getZaposleni())) {
                        Hibernate.initialize(p.getObavestenjas());
                        Hibernate.initialize(p.getSpiskovis());
                        Hibernate.initialize(p.getFajlovis());
                        Hibernate.initialize(p.getLabVezbes());
                        Hibernate.initialize(p.getDomacis());
                        Initializer.initLab(p);
                        Initializer.initDomaci(p);
                    }
                    profLogedIn = true;
                    currUserLabel = currUser.getZaposleni().getIme() + " " + currUser.getZaposleni().getPrezime();
                    t.commit();
                } else if (currUser.getRole().getDescription().equals("student")) {
                    Hibernate.initialize(currUser.getStudent());
                    Hibernate.initialize(currUser.getRole());
                    Initializer.initPredmets(currUser.getStudent());
                    for (Predmet p : Initializer.toList(currUser.getStudent())) {
                        Hibernate.initialize(p.getObavestenjas());
                        Hibernate.initialize(p.getSpiskovis());
                        Hibernate.initialize(p.getFajlovis());
                        Hibernate.initialize(p.getLabVezbes());
                        Hibernate.initialize(p.getDomacis());
                        Hibernate.initialize(p.getKatedra());
                        Initializer.initLab(p);
                        Initializer.initDomaci(p);
                    }
                    studentLogedIn = true;
                    currUserLabel = currUser.getStudent().getIme() + " " + currUser.getStudent().getPrezime();
                    t.commit();
                } else if (currUser.getRole().getDescription().equals("admin")) {
                    adminLogedIn = true;
                    currUserLabel = currUser.getKorisnickoIme();

                }
                return "home?faces-redirect=true";
            }
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
        return "error?faces-redirect=true";
    }

    public String logOut() {
        profLogedIn = false;
        studentLogedIn = false;
        adminLogedIn = false;
        currUserLabel = "";
        HttpSession httpSes = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        httpSes.invalidate();
        currUser = null;
        return "home?faces-redirect=true";
    }
}
